package IOhomework;

import java.io.*;
import java.util.Locale;

public class Test {
    // 1. 写一个方法，实现文件的拷贝
    public void copy() {
        FileReader fr = null;
        String str=null;
        try {
            fr = new FileReader("d:/1.txt");
            int a = 0;
            while ((a = fr.read()) != -1) {
                str+=(char)a;
                System.out.print((char)a);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream("d:/txt/copy1.txt");
            fos.write(str.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    // 2. 输入一段字符串，将该字符串追加到指定文本文件中
    public void add(){
        FileWriter fw=null;
        try {
            fw=new FileWriter("d:/txt/3.txt");
            String str="小爱同学";
            fw.append(str.toLowerCase(Locale.ROOT));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    public static void main(String[] args) {
        Test test = new Test();
        test.copy();
        test.add();
    }
}
